package com.atguigu.gmall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.item.service.ItemService;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

/**
 * @author atguigu-mqx
 */
@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ProductFeignClient productFeignClient;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private ListFeignClient listFeignClient;

    @Override
    public Map<String, Object> getItem(Long skuId) {
        //  声明的集合对象
        HashMap<String, Object> map = new HashMap<>();
        //  使用异步编排完成任务：
        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //  获取数据
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            map.put("skuInfo", skuInfo);
            return skuInfo;
        },threadPoolExecutor);

        //  获取商品的价格
        CompletableFuture<Void> priceCompletableFuture = CompletableFuture.runAsync(() -> {
            //  获取商品的价格
            BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
            map.put("price", skuPrice);
        }, threadPoolExecutor);

        //  获取分类名称登录数据
        CompletableFuture<Void> categoryViewCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
            map.put("categoryView", categoryView);
        }, threadPoolExecutor);

        //  获取规格参数： 平台属性 平台属性值
        CompletableFuture<Void> attrCompletableFuture = CompletableFuture.runAsync(() -> {
            List<BaseAttrInfo> attrList = productFeignClient.getAttrList(skuId);
            //  使用拉姆达表达式
            List<Map<String, String>> skuAttrList = attrList.stream().map(baseAttrInfo -> {
                //  创建一个map
                Map<String, String> attrMap = new HashMap<>();
                attrMap.put("attrName", baseAttrInfo.getAttrName());
                //  不懂了！
                attrMap.put("attrValue", baseAttrInfo.getAttrValueList().get(0).getValueName());
                //为什么放入map  前端取值方式：${skuAttr.attrName}  ${skuAttr.attrValue}
                return attrMap;
            }).collect(Collectors.toList());
            //  规格：需要获取的是 attrName, attrValue
            map.put("skuAttrList", skuAttrList);
        },threadPoolExecutor);

        //  点击切换
        CompletableFuture<Void> valuesCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            Map idsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            String strJson = JSON.toJSONString(idsMap); // {"3716|3718":"18","3717|3718":"17"}
            map.put("valuesSkuJson", strJson);
        }, threadPoolExecutor);

        //  获取到销售属性+销售属性值并锁定
        CompletableFuture<Void> spuSaleCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            List<SpuSaleAttr> spuSaleAttrList = productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
            map.put("spuSaleAttrList", spuSaleAttrList);
        }, threadPoolExecutor);

        //  获取海报信息！
        CompletableFuture<Void> spuPosterCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            List<SpuPoster> spuPosterList = productFeignClient.getSpuPosterBySpuId(skuInfo.getSpuId());
            map.put("spuPosterList", spuPosterList);
        }, threadPoolExecutor);

        //根据 热度排名
        CompletableFuture<Void>incrHotCompletableFuture = CompletableFuture.runAsync(() -> {
            listFeignClient.incrHotScore(skuId);
        }, threadPoolExecutor);

        //  需要将所有的异步编排线程进行组合！
        CompletableFuture.allOf(skuInfoCompletableFuture,
                priceCompletableFuture,
                categoryViewCompletableFuture,
                attrCompletableFuture,
                valuesCompletableFuture,
                spuSaleCompletableFuture,
                spuPosterCompletableFuture,
                incrHotCompletableFuture
        ).join();
        //  返回数据
        return map;
    }

 }

